package com.demo.phone;

import com.alibaba.fastjson.JSON;
import com.demo.phone.config.NewConfig;
import com.demo.phone.req.BaseRequest;
import com.demo.utils.HttpClientResult;
import com.demo.utils.HttpClientUtil;
import com.demo.utils.ParseMD5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @author : YU.TAN
 * @date : 2021/10/27 10:42
 */
@Component
public class SendUtils {


    @Autowired
    private NewConfig newConfig;

    private void sendGet(BaseRequest<Object> request, String url) {
        long time = System.currentTimeMillis() / 1000;
        request.setAppKey(newConfig.getAppKey());
        request.setAppSecret(newConfig.getAppSecret());
        request.setTimeStamp(String.valueOf(time));

        Map<String, Object> map = new HashMap<>(16);
        map.put("param", request.getT());
        String data = JSON.toJSONString(map);

        String param = JSON.toJSONString(request.getT());
        request.setData(data);
        request.setParam(param);

        String sign = ParseMD5.parseStrToMd5U32(getStr(request));


        Map<String, String> params = new HashMap<>(16);
        params.put("appKey", request.getAppKey());
        params.put("timeStamp", request.getTimeStamp());
        params.put("sign", sign);
        params.put("data", request.getData());
        params.put("brandName", request.getBrandName());

        try {
            HttpClientResult result = HttpClientUtil.doGet(request.getUrl(), params);
        } catch (Exception e) {
            e.printStackTrace();
        }


    }


    private void sendPost(BaseRequest<Object> request, String url) {
        long time = System.currentTimeMillis() / 1000;
        request.setAppKey(newConfig.getAppKey());
        request.setAppSecret(newConfig.getAppSecret());
        request.setTimeStamp(String.valueOf(time));

        Map<String, Object> map = new HashMap<>(16);
        map.put("param", request.getT());
        String data = JSON.toJSONString(map);

        String param = JSON.toJSONString(request.getT());
        request.setData(data);
        request.setParam(param);

        String sign = ParseMD5.parseStrToMd5U32(getStr(request));


        Map<String, String> params = new HashMap<>(16);
        params.put("appKey", request.getAppKey());
        params.put("timeStamp", request.getTimeStamp());
        params.put("sign", sign);
        params.put("data", request.getData());
        params.put("brandName", request.getBrandName());

        try {
            HttpClientResult result = HttpClientUtil.doPost(request.getUrl(), params);
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    public static String getStr(BaseRequest<Object> baseRequest) {
        String result = "%sappKey=%sparam=%stimestamp=%s%s";
        return String.format(result, baseRequest.getAppSecret(), baseRequest.getAppKey(), baseRequest.getParam(), baseRequest.getTimeStamp(), baseRequest.getAppSecret());
    }
}
